<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('financial_transactions', function (Blueprint $table) {
            $table->id();
            $table->timestamps();

            // 交易基本信息
            $table->string('transaction_number', 50)->unique(); // 交易编号
            $table->string('transaction_type', 50); // expense, compensation, reimbursement, etc.
            $table->string('related_module', 50); // work_injury, work_death, salary, etc.
            $table->string('related_record_type', 50)->nullable(); // work_injury_records, work_related_deaths, etc.
            $table->unsignedBigInteger('related_record_id')->nullable();

            // 金额信息
            $table->decimal('amount', 12, 2); // 交易金额
            $table->string('currency', 10)->default('CNY'); // 货币类型
            $table->string('payment_method', 50); // bank_transfer, cash, check, etc.

            // 收款人信息
            $table->string('recipient_name', 100); // 收款人姓名
            $table->string('recipient_type', 50); // individual, company, hospital, etc.
            $table->string('recipient_id_card', 18)->nullable(); // 收款人身份证号
            $table->string('recipient_bank_account', 50)->nullable(); // 银行账号
            $table->string('recipient_bank_name', 100)->nullable(); // 开户银行
            $table->string('recipient_contact', 100)->nullable(); // 联系方式

            // 交易事由
            $table->text('transaction_purpose'); // 交易事由
            $table->string('expense_category', 50)->nullable(); // 费用类别：medical, compensation, transportation, etc.
            $table->string('expense_subcategory', 50)->nullable(); // 费用子类

            // 关联员工信息
            $table->unsignedBigInteger('employee_id')->nullable();
            $table->string('employee_name', 100)->nullable();
            $table->string('employee_number', 50)->nullable();
            $table->unsignedBigInteger('department_id')->nullable();
            $table->string('department_name', 100)->nullable();

            // 审批信息
            $table->string('approval_status', 20)->default('pending'); // pending, approved, rejected
            $table->unsignedBigInteger('approved_by')->nullable();
            $table->string('approved_by_name', 100)->nullable();
            $table->datetime('approved_at')->nullable();
            $table->text('approval_notes')->nullable();

            // 支付信息
            $table->string('payment_status', 20)->default('pending'); // pending, paid, cancelled, failed
            $table->datetime('payment_date')->nullable(); // 支付日期
            $table->string('payment_reference', 100)->nullable(); // 支付凭证号
            $table->string('payment_batch_number', 50)->nullable(); // 支付批次号

            // 凭证信息
            $table->json('receipts')->nullable(); // 发票、收据等凭证文件
            $table->string('receipt_type', 50)->nullable(); // paper, electronic, photo, signature
            $table->boolean('has_physical_receipt')->default(false); // 是否有纸质凭证
            $table->boolean('has_digital_signature')->default(false); // 是否有数字签名
            $table->string('signature_method', 50)->nullable(); // 签名方式：handwritten, digital, remote

            // 财务信息
            $table->string('budget_category', 50)->nullable(); // 预算类别
            $table->string('cost_center', 50)->nullable(); // 成本中心
            $table->string('accounting_period', 20)->nullable(); // 会计期间 YYYY-MM
            $table->boolean('is_tax_deductible')->default(false); // 是否可抵扣税款

            // 异常标记
            $table->boolean('is_exception')->default(false);
            $table->string('exception_type', 50)->nullable(); // amount_exceeds_limit, no_approval, invalid_receipt, etc.
            $table->text('exception_reason')->nullable();
            $table->string('investigation_status', 20)->nullable(); // pending, investigating, resolved

            // 状态管理
            $table->string('status', 20)->default('draft'); // draft, submitted, approved, paid, completed, cancelled
            $table->boolean('is_reversible')->default(false); // 是否可撤销
            $table->datetime('reversal_date')->nullable(); // 撤销日期
            $table->text('reversal_reason')->nullable(); // 撤销原因

            // 审计信息
            $table->json('audit_trail')->nullable(); // 操作历史记录
            $table->datetime('last_audited_at')->nullable();
            $table->string('last_audited_by', 100)->nullable();

            // 备注和附件
            $table->text('notes')->nullable();
            $table->json('attachments')->nullable(); // 相关附件文件

            // 创建者信息
            $table->string('created_by', 100)->nullable();
            $table->string('updated_by', 100)->nullable();

            // 索引
            $table->index(['transaction_type', 'payment_status'], 'ft_ttype_pstatus_idx');
            $table->index(['related_module', 'related_record_id'], 'ft_module_record_idx');
            $table->index(['employee_id', 'created_at'], 'ft_emp_created_idx');
            $table->index(['recipient_name'], 'ft_recipient_idx');
            $table->index(['payment_date'], 'ft_pdate_idx');
            $table->index(['approval_status'], 'ft_astatus_idx');
            $table->index(['status'], 'ft_status_idx');
            $table->index(['is_exception'], 'ft_exception_idx');
            $table->index(['transaction_number'], 'ft_tnumber_idx');

            // 外键约束
            $table->foreign('employee_id')->references('id')->on('employees')->onDelete('set null');
            $table->foreign('department_id')->references('id')->on('departments')->onDelete('set null');
            $table->foreign('approved_by')->references('id')->on('users')->onDelete('set null');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('financial_transactions');
    }
};
